package command

import (
	"strings"
	"time"

	"github.com/tidwall/sjson"

	"ssdevice/common/dto"
	"ssdevice/common/utils"
	"ssdevice/internal/device/models"
)

type SensorInsertReq struct {
	Id           string                  `json:"id"`
	Sn           string                  `json:"sn"`
	ProductModel string                  `json:"productModel"`
	Key          string                  `json:"key" vd:"len($)>0"`
	Label        string                  `json:"label"`
	Type         string                  `json:"type"`
	Data         interface{}             `json:"data"`
	Index        *int                    `json:"index"`
	Config       *map[string]interface{} `json:"config"`
	Param        *map[string]interface{} `json:"param"`
	dto.UserInfo `search:"-"`
}

func (d *SensorInsertReq) Generate(model *models.SysDevSensor) {
	model.Id = utils.SimpleUUID()
	model.Sn = strings.TrimSpace(d.Sn)
	model.ProductModel = strings.TrimSpace(d.ProductModel)
	model.Key = strings.TrimSpace(d.Key)
	model.Label = strings.TrimSpace(d.Label)
	if len(model.Label) == 0 {
		model.Label = model.Key
	}
	model.Type = d.Type
	model.Data = utils.MapToBytes(map[string]interface{}{model.Key: d.Data})
	model.Index = d.Index

	schema := utils.GetObjectSchema(utils.BytesToMap(model.Data))["properties"].(map[string]interface{})
	schemaStr := string(utils.MapToBytes(schema))
	schemaStr, _ = sjson.Set(schemaStr, model.Key+".title", model.Label)
	model.Schema = []byte(schemaStr)

	var tmpConfig map[string]interface{}
	if d.Config != nil {
		tmpConfig = map[string]interface{}{model.Key: *d.Config}
	} else {
		tmpConfig = utils.GetObjectConfig(utils.BytesToMap(model.Data))
	}
	model.Config = utils.MapToBytes(tmpConfig)
	if d.Param != nil {
		model.Param = utils.MapToBytes(*d.Param)
	}
	now := dto.LocalTime(time.Now())
	model.CreateTime = &now
}

type SensorEditReq struct {
	Id           string                  `json:"id" vd:"len($)>0"`
	Key          string                  `json:"key" vd:"len($)>0"`
	Label        string                  `json:"label"`
	Type         string                  `json:"type"`
	Data         interface{}             `json:"data"`
	Index        *int                    `json:"index"`
	Config       *map[string]interface{} `json:"config"`
	Param        *map[string]interface{} `json:"param"`
	ConfDel      string                  `json:"confDel"`
	dto.UserInfo `search:"-"`
}

func (d *SensorEditReq) Generate(model *models.SysDevSensor) {
	if len(d.ConfDel) > 0 {
		configStr := string(model.Config)
		configStr, _ = sjson.Delete(configStr, d.ConfDel)
		model.Config = []byte(configStr)
		// configDel单独传参
		return
	}
	// 可编辑项
	if len(d.Key) > 0 {
		model.Key = strings.TrimSpace(d.Key)
	}
	if len(d.Label) > 0 {
		model.Label = d.Label
	}
	if len(d.Type) > 0 {
		model.Type = d.Type
	}
	if d.Data != nil {
		data := map[string]interface{}{model.Key: d.Data}
		model.Data = utils.MapToBytes(data)
		schema := utils.GetObjectSchema(data)["properties"].(map[string]interface{})
		schemaStr := string(utils.MapToBytes(schema))
		schemaStr, _ = sjson.Set(schemaStr, model.Key+".title", model.Label)
		model.Schema = []byte(schemaStr)
	}
	if d.Index != nil {
		model.Index = d.Index
	}
	if d.Config != nil {
		model.Config = utils.MapToBytes(map[string]interface{}{model.Key: *d.Config})
	}
	if d.Param != nil {
		model.Param = utils.MapToBytes(*d.Param)
	}
}

type SensorDeleteReq struct {
	Id  string `json:"id" vd:"len($)>0"`
	Key string `json:"key"`

	dto.UserInfo `search:"-"`
}
